package com.dlut.java;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiuHeng
 * @Description:
 * @Date: 2022-09-04 23:46
 */
public class RestoreIp {
    public List<String> restoreIpAddresses(String s) {
        int n = s.length();
        String[] ss = new String[4];
        List<String> list = new ArrayList<>();
        for (int i = 1; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                for (int k = j+1; k < n; k++) {
                    ss[0] = s.substring(0, i);
                    ss[1] = s.substring(i, j);
                    ss[2] = s.substring(j, k);
                    ss[3] = s.substring(k, n);
                    if (isIp(ss))
                        list.add(ss[0] + "." + ss[1] + "." + ss[2] + "." + ss[3]);
                }
            }
        }
        return list;
    }
    public static boolean isIp(String[] s){
        for (int i = 0; i < s.length; i++) {
            String ss = s[i];
            if (ss.length()>3 || (ss.length()>1 && ss.charAt(0) == '0'))
                return false;
            int i1 = Integer.parseInt(ss);
            if (i1<0 || i1 >255){
                return false;
            }
        }
        return true;
    }
}
